﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Task.BLL;
using Yipusoft.Task.Model;
using System.Text;

public partial class Task_ChartOfScore : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindFirstList();
            StartDate.Text = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToShortDateString();
            EndDate.Text = DateTime.Now.ToShortDateString();
            BindData();
        }
    }
    private void BindFirstList()
    {
        string Departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        FirstList.Items.Clear();
        if (Departmentid == "1300050")
        {
            ListItem li;
            DepartmentInfo info = Department.GetDepartment(Departmentid);
            li = new ListItem();
            li.Text = info.ShortName;
            li.Value = info.DepartmentID;
            FirstList.Items.Add(li);

        }
        else
        {
            ListItem li;
            string sql = "SELECT * FROM Account_Departments WHERE (SUBSTRING(DepartmentID, 1, 6) = '" + Departmentid.Substring(0, 6) + "') AND (SUBSTRING(DepartmentID, 7, 1) != '0')";
            DataView dv = SplitPage.GetPageData(sql, 0, 10000);
            foreach (DataRowView drv in dv)
            {
                li = new ListItem();
                li.Text = drv["ShortName"].ToString();
                li.Value = drv["DepartmentID"].ToString();
                FirstList.Items.Add(li);
            }
        }

        FirstList.SelectedValue = Departmentid;
    }
    protected void BindData()
    {
        StringBuilder sql = new StringBuilder();
        sql.Append("Select ShortName,Sum(Score) as Score From View_TaskUsers1 Where ParentId=0");
        if (FirstList.SelectedValue != "")
        {
            sql.Append(" And CreateDepartment='" + FirstList.SelectedValue + "'");
        }
        sql.Append(" and ((StartDate>='" + StartDate.Text + "' and StartDate<='" + EndDate.Text + "')");
        sql.Append(" or (StartDate<'" + EndDate.Text + "' and EndDate>='" + EndDate.Text + "'))");
        sql.Append(" Group By Left(UserId,2),ShortName Order By Left(UserId,2)");
        DataView dv = SplitPage.GetPageData(sql.ToString(), 0, 1000);

        if (FirstList.SelectedValue != "")
        {
            UserChart.Titles["Title"].Text = "各单位关于（" + FirstList.SelectedItem.Text + "）任务得分分析\n\n" + DateTime.Parse(StartDate.Text).ToLongDateString() + "至" + DateTime.Parse(EndDate.Text).ToLongDateString();
        }
        else
        {
            UserChart.Titles["Title"].Text = "各单位任务得分分析\n\n" + DateTime.Parse(StartDate.Text).ToLongDateString() + "至" + DateTime.Parse(EndDate.Text).ToLongDateString();
        }
        UserChart.ChartAreas["Default"].AxisX.LabelsAutoFit = false;
        UserChart.Series["得分"].Points.DataBindXY(dv, "ShortName", dv, "Score");
    }
    protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }
    protected void ChartList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ChartList.SelectedValue == "1")
        {
            UserChart.Series["得分"].Type = Dundas.Charting.WebControl.SeriesChartType.Column;
        }
        else
        {
            UserChart.Series["得分"].Type = Dundas.Charting.WebControl.SeriesChartType.Radar;
        }
        BindData();
    }
}
